package cn.yl.fxboot.application.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;

public class Common {
	public static final String RE_URLS = "(https://|http://)?([\\w-]+\\.)+[\\w-]+(:\\d+)*(/[\\w- ./?%&=]*)?";
	private static Random random = new Random();
    public static Document jsonpConnect(String url, Boolean isMobile) {
        Document doc = null;
        if (StrUtil.isNotEmpty(url)) {
            try {
                Connection connect = Jsoup.connect(ReUtil.get(Common.RE_URLS, url, 0));
                if (isMobile) {
                    connect.headers(Common.getMobileHeaders());
                }
                doc = connect.timeout(5000).get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return doc;
    }
 
    public static String getLocationUrl(String url) {
        HashMap<String, String> headers = getMobileHeaders();
        HttpResponse execute = HttpUtil.createGet(url).addHeaders(headers).execute();
        String redirectUrl = execute.header("Location");
        return redirectUrl;
    }
 
    public static HashMap<String, String> getMobileHeaders() {
        HashMap<String, String> headers = MapUtil.newHashMap();
        headers.put("User-Agent", agents().get(random.nextInt(agents().size()-1)));
        return headers;
    }
    
    private static ArrayList<String> agents() {
    	ArrayList<String> list = new ArrayList<>();
    	list.add("Mozilla/5.0 (iPad; U; CPU OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5");
    	list.add("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
    	list.add("Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10");
    	list.add("MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1");
    	list.add("Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5");
    	list.add("Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X; zh-CN) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B554a UCBrowser/9.3.1.339 Mobile");
    	list.add("Mozilla/5.0 (iPhone; CPU iPhone OS 7_0_4 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Mobile/11B554a Safari/7534.48.3");
    	list.add("MQQBrowser/3.5/Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M9 Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
    	list.add("MQQBrowser/3.7/Adr (Linux; U; 4.0.3; zh-cn; M9 Build/Flyme 1.0.1;640*960)");
    	list.add("MQQBrowser/4.0/Mozilla/5.0 (Linux; U; Android 4.0.3; zh-cn; M031 Build/IML74K) AppleWebKit/533.1 (KHTML, like Gecko) Mobile Safari/533.1");
    	return list;
    }

}
